UserInfo.init();
var gridListOne, gridListTwo, gridListThree;
var layform;
var id = UserInfo.userId; //当前操作者id
var oneindex = 0,twoindex = 0, threeindex = 0;
$(document).ready(function () {
    layui.use(['form'], function() {
        layform = layui.form;
        var one = document.getElementById("one");
        var two = document.getElementById("two");
        var three = document.getElementById("three");


        gridListOne = new dataGrid("gridListOne");
        gridListTwo = new dataGrid("gridListTwo");
        gridListThree = new dataGrid("gridListThree");

        initData();
        layform.on('radio(statusp)', function(data){
            switch (data.value) {
                case '1':
                    oneindex += 1;
                    one.style.display='block';
                    two.style.display='none';
                    three.style.display='none';
                    gridListOne.reload({
                        'createUser':id,
                        "sysTaskKeyState": data.value
                    });
                    break;
                case '2':
                    twoindex += 1;
                    one.style.display='none';
                    two.style.display='block';
                    three.style.display='none';
                    if(twoindex == 1){
                        gridListTwo.load({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }else{
                        gridListTwo.reload({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }
                    break;
                case '3':
                    threeindex += 1;
                    one.style.display='none';
                    two.style.display='none';
                    three.style.display='block';
                    if(threeindex == 1){
                        gridListThree.load({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }else{
                        gridListThree.reload({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }
                    break;
            }

        });
    });
});

function initData() {
    gridListOne.load({
        'createUser':id,
        'sysTaskKeyState':1
    });
}


function formatTitle(e) {
    var row = e.record;
    var t = "";
    var wfTitle = row.wfTitle;
    var record ;
    ActHttpRequest("/wf/systask/findById",{"id": row.currentSysTaskId}, function (data) {
        if(data.OK == "OK"){
            record = JSON.parse(data.wfSystask.sysTaskRecord.replace(/\'/g,"\""));
            if(record == "" || record == undefined){
                t="";
            }else{
                var index_1 = wfTitle.indexOf("{{"),
                    index_2 = wfTitle.indexOf("}}");
                var firstTitle = wfTitle.substring(0, index_1);
                var secondTitle = wfTitle.substring(index_2 + 2);
                var title = wfTitle.substring(index_1 + 2, index_2);

                record.forEach(function (data) {
                    if(data.name == title){
                        t = data.values[0];
                    }
                });
                t = firstTitle + t + secondTitle;
            }
        }
    });

    return t;
}


function formatOper(e) {
    var row = e.record;
    return "<a class='layui-btn layui-btn-sm' hideFocus='' href='javascript:void(0)' onClick='open_Detail(\"" + row.baseTaskId + "\",\"" + row.wfId + "\",\""+row.id+"\")'><i class='layui-icon'>&#xe629;</i>查看</a>";
}

function open_Detail(baseTaskId, wfId, kid) {
    showProgressView(wfId, kid, baseTaskId, "", "", 'viewTimeline.html', 'viewFlowchat.html', 'viewForm.html');
}


function formatModuleId(e) {
    var row=e.record;
    var moduleName = "";
    ActHttpRequest("/wf/module/findById", {"id":row.moduleId}, function (d) {
        moduleName =  d.wfModule.moduleName;
    });
    return moduleName;
}

function formatUserId(e) {
    var row = e.record;
    var userName = "";
    ActHttpRequest("/sys-user/"+row.createUser, {}, function (d) {
        userName = d.sysUser.userName;
    });
    return userName;
}


function formatActivityId(e) {
    var row=e.record;
    var activityName = "";
    ActHttpRequest("/wf/activity/"+row.activityId, {}, function (d) {
        if(d.OK === "OK"){
            if(d.wfActivity){
                activityName =  d.wfActivity.activityName;
            }
        }
    });
    return activityName;
}

function formatSysTaskKeyState(e) {
    var row=e.record;
    var state = "";
    switch (row.sysTaskKeyState) {
        case 1:
            state="办理中";
            break;
        case 2:
            state="已办结";
            break;
        case 3:
            state="已撤销";
            break;
    }
    return state;
}